package java_concurrency_program_design_in_practice.atomic_reference;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 无锁的对象引用<br/>
 * 它可以保证你在修改对象引用时的安全性<br/>
 * @author lixiangke
 * @date 2017/12/8
 */
public class AtomicReferenceDemo2 {
    static AtomicReference<Student> student = new AtomicReference<>(new Student(1, "libai", 18));

    public static void main(String[] args) {
        boolean flag = student.compareAndSet(new Student(1, "libai", 18),
                new Student(1, "libai", 20));
        System.out.println(flag);
        System.out.println(student.get().getAge());

        boolean flag2 = student.compareAndSet(student.get(),
                new Student(1, "libai", 20));
        System.out.println(flag2);
        System.out.println(student.get().getAge());
    }

}
